﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class ReplyService : IReplyService
    {
        private IReplyManager _ir;
        public ReplyService(IReplyManager ir)
        {
            _ir = ir;
        }
        public async Task<int> AddReplyAsync(int pid, string content, int parentId, int likesum, int uid)
        {
            return await _ir.AddAsync(new Model.Reply 
            {
                Pid = pid,
                Content = content,
                ParentId = parentId,
                LikeSum = likesum,
                Uid = uid
            });
        }

        public async Task<int> DeleteReplyAsync(int id)
        {
            var data = await _ir.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _ir.DeleteAsync(data);
        }

        public async Task<List<ReplyDto>> GetAllReplyAsync(int deleteId = 1)
        {
            var data = await _ir.GetChooseAsync(re => re.DeleteId == deleteId);
            return data == null ? new List<ReplyDto>() : data.Select(re => new ReplyDto 
            {
                Id = re.Id,
                Pid = re.Pid,
                Content = re.Content,
                ParentId = re.ParentId,
                LikeSum=re.LikeSum,
                Uid = re.Uid,
                CreateTime = re.CreateTime,
                UpdateTime = re.UpdateTime
            }).ToList();
        }

        public async Task<ReplyDto> GetByIdAsync(int id)
        {
            var data = await _ir.GetById(id);
            return data == null ? new ReplyDto() : new ReplyDto 
            {
                Id = data.Id,
                Pid = data.Pid,
                Content = data.Content,
                ParentId = data.ParentId,
                LikeSum = data.LikeSum,
                Uid = data.Uid,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime
            };
        }

        public async Task<List<ReplyDto>> GetByNameAsync(string content, int deleteId = 1)
        {
            var data = await _ir.GetChooseAsync(re => re.Content.Contains(content) && re.DeleteId == deleteId);
            return data == null ? new List<ReplyDto>() : data.Select(re => new ReplyDto 
            {
                Id = re.Id,
                Pid = re.Pid,
                Content = re.Content,
                ParentId = re.ParentId,
                LikeSum = re.LikeSum,
                Uid = re.Uid,
                CreateTime = re.CreateTime,
                UpdateTime = re.UpdateTime
            }).ToList();
        }

        public async Task<Dictionary<int, List<ReplyDto>>> GetByReplyInfoListAsync(List<CommentDto> Comment)
        {
            Dictionary<int,List<ReplyDto>> r =new Dictionary<int, List<ReplyDto>>();
            //id 4 pid 4 
                foreach (var a in Comment) 
                {
                    var rep = await _ir.GetChooseAsync(r => r.Pid == a.Id);
                    

                    r.Add(a.Id,(rep != null ? rep.Select(re => new ReplyDto
                    {
                        Id = re.Id,
                        LikeSum = re.LikeSum,
                        ParentId = re.ParentId,
                        Uid = re.Uid,
                        Content = re.Content,
                        Pid = re.Pid,
                        CreateTime = re.CreateTime
                    }).ToList() : new List<ReplyDto>()));
                }
            
            return r;
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _ir.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _ir.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _ir.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _ir.UpdateAsync(data);
        }

        public async Task<int> UpdateReplyAsync(int id, int pid, string content, int parentId, int likesum, int uid)
        {
            var data = await _ir.GetById(id);
            if (data == null)
                return -1;
            data.ParentId = parentId;
            data.Content = content;
            data.ParentId = parentId;
            data.LikeSum = likesum;
            data.Uid = uid;
            data.UpdateTime = DateTime.Now;
            return await _ir.UpdateAsync(data);
        }
    }
}
